import heapq
class Solution(object):
    def maxPerformance(self, n, speed, efficiency, k):
        nums = [[efficiency[i], speed[i]] for i in range(n)]
        nums.sort(reverse=True)
        st = []
        value = 0
        ans = 0
        base = 10 ** 9 + 7
        for i in range(k):
            value += nums[i][1]
            ans = max(ans, value * nums[i][0])
            st.append(nums[i][1])
        heapq.heapify(st)
        for i in range(k, n):
            value += nums[i][1]
            heapq.heappush(st, nums[i][1])
            node = heapq.heappop(st)
            value -= node
            ans = max(ans, value * nums[i][0])
        return ans % base


data = Solution()
n = 6
speed = [2, 10, 3, 1, 5, 8]
efficiency = [5, 4, 3, 9, 7, 2]
k = 2
print(data.maxPerformance(n, speed, efficiency, k))
